Load shapefile

# Load FL eviction data. Source: https://data-downloads.evictionlab.org/
FL_counties <- readOGR("FL_eviction_counties.geojson")
## OGR data source with driver: GeoJSON 
## Source: "C:\Users\Lindsay Graff\Documents\R Shiny\HW2-lgraff\FL_eviction_counties.geojson", layer: "FL_eviction_counties"
## with 67 features
## It has 398 fields
plot(FL_counties)

# Select a subset of columns
# poverty rate, median gross rent, median property value, num of evictions, eviction rate
FL_16 <- FL_counties
FL_16@data <- FL_16@data %>% 
  dplyr::select(GEOID, west, south, north, n, pl, pr.16, mgr.16, mpv.16, e.16, er.16)

Basemap

leaflet() %>%
  addProviderTiles(providers$OpenStreetMap)  
  #addPolygons(data = FL_16, weight = 1)

Map with marker points

# Calculate centroids of counties to find lat and long
FL_polyset <- SpatialPolygons2PolySet(x = FL_16)
centroid <- calcCentroid(FL_polyset, rollup = 1)

# Bind county centroids to Florida data
df_FL <- cbind(FL_16@data, centroid)

# Map with circle markers, radius defined by poverty rate
# Include a popup that lists the county's poverty rate (%)
leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>% 
  addPolygons(data = FL_16, weight = 1) %>% 
  addCircleMarkers(data = df_FL, lat = ~Y, lng = ~X, 
                   radius = ~log(pr.16),
                   popup = ~as.character(pr.16),
                   color = "red") 

Map with layer of lines

# Map with layer of lines
leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>% 
  addPolylines(data = FL_16, weight = 1) 

Plain polygons map with no additions

# Map with layer of polygons, no additions
leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>% 
  addPolygons(data = FL_16, weight = 1)

Polygons colored by median gross rent

# Make color palette for median gross rent (mgr)
bins <- c(500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400)
pal <- colorBin("YlOrRd", domain = df_FL$mgr.16, bins = bins)

# Map with layer of polygons, colored by mgr
leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>% 
  addPolygons(data = FL_16, weight = 1,
              fillColor = ~pal(mgr.16),
              fillOpacity = 0.8) %>% 
  addLegend(title = "Median Gross Rent", pal = pal, values = FL_16@data$mgr.16,
            labFormat = labelFormat(prefix = "$"), opacity = 1)

Add layersControl with two basemaps and two different layers

# Create a palette for MPV and evictions
qpal_MPV <- colorQuantile("Blues", df_FL$mpv.16, n = 5)
qpal_Evic <- colorQuantile("Reds", df_FL$e.16, n = 5, na.color = "white")

# Create two basemaps with layers control
leaflet() %>%
  addProviderTiles(providers$OpenStreetMap, group = "Open Street") %>% 
  addProviderTiles(providers$Esri.WorldStreetMap, group = "Esri World Street") %>% 
  
  #addLayersControl(
   # baseGroups = c("Open Street", "Esri World Street"),
  #  options = layersControlOptions(collapsed = FALSE)
  #) %>% 

  # Median Property Value polygons
  addPolygons(data = FL_16, weight = 1,
              smoothFactor = .2,
              fillOpacity = .8,
              fillColor = ~qpal_MPV(df_FL$mpv.16), group = "Median Property Value") %>% 
  addLegend(title = "Median Property Value", pal = qpal_MPV, values = df_FL$mpv.16,
            group = "Median Property Value") %>% 
  
  # Evictions polygons
  addPolygons(data = FL_16, weight = 1,
              smoothFactor = .2,
              fillOpacity = .8,
              fillColor = ~qpal_Evic(df_FL$e.16), group = "Evictions") %>% 
  addLegend(title = "Evictions", pal = qpal_Evic, values = df_FL$e.16,
            group = "Evictions") %>% 
  
  # Layers control for polygons
  addLayersControl(overlayGroups = c("Median Property Value", "Evictions"),
                   c("Open Street", "Esri World Street"),
                   options = layersControlOptions(collapsed = F)) %>% 
  
  # Make "Evictions" default selection 
  hideGroup("Median Property Value")